﻿
@{
    ViewData["Title"] = "TaskAuthManage";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">

            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">验证方式名称：</label>
                        <div class="layui-input-inline mr0">
                            <input id="authName" name="authName" class="layui-input" type="text" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchTbBas" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <a class="layui-btn icon-btn" id="addAuth">
                            <i class="layui-icon">&#xe654;</i>新增验证方式
                        </a>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableTbBas" lay-filter="tableTbBas"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelAuth">
    <form id="authEditForm" lay-filter="authEditForm" class="layui-form model-form">
        <input type="hidden" name="authGuid" id="authGuid">
        <div class="layui-form-item layui-row">
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">验证方式名</label>
                <div class="layui-input-block">
                    <input name="authName" placeholder="请输入验证方式名称" type="text" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">接口地址</label>
                <div class="layui-input-block">
                    <input name="authUrl" placeholder="请输入接口地址" type="text" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">用户名字段</label>
                <div class="layui-input-block">
                    <input name="userNameField" placeholder="请输入用户名字段" type="text" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input name="userName" placeholder="请输入用户名" type="text" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">密码字段</label>
                <div class="layui-input-block">
                    <input name="pswField" placeholder="请输入用户名字段" type="text" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input name="psw" placeholder="请输入密码" type="password" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">Token字段</label>
                <div class="layui-input-block">
                    <input name="authTokenField" placeholder="请输入验证Token所在的字段" type="text" class="layui-input" />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">Token头</label>
                <div class="layui-input-block">
                    <input name="authTokenHeader" placeholder="请输入接口请求Token写入的头字段" type="text" class="layui-input" />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">调用方式</label>
                <div class="layui-input-block">
                    <select name="method" lay-verType="tips" lay-verify="required" required>
                        <option value="get">Get</option>
                        <option value="post">Post</option>
                        <option value="put">Put</option>
                        <option value="delete">Delete</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">参数类型</label>
                <div class="layui-input-block">
                    <select name="bodyType" lay-verType="tips" lay-verify="required" required>
                        <option value="1">FromBody</option>
                        <option value="2">FromForm</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline layui-col-md12">
                <label class="layui-form-label">其他请求参数(json)</label>
                <div class="layui-input-block">
                    <textarea name="requestBody" id="requestBody" class="layui-textarea" style="height:90px;"></textarea>
                </div>
            </div>
            <div class="layui-inline layui-col-md12 headerAuth">
                <label class="layui-form-label">额外请求头(json)</label>
                <div class="layui-input-block">
                    <textarea name="token" id="token" class="layui-textarea" style="height:90px;"></textarea>
                </div>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="saveAuth" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['admin', 'layer', 'form', 'table', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var admin = layui.admin;
        form.render('select');
        // 渲染表格
        var insTb = table.render({
            elem: '#tableTbBas',
            url: '/api/Quartz/QuaryTaskAuthPage',
            page: true,
            limit: 20,
            cellMinWidth: 100,
            cols: [[
                { field: 'authName', align: 'center', title: '验证方式名称' },
                { field: 'authUrl', align: 'center', title: '验证接口' },
                { field: 'method', align: 'center', title: '请求方式' },
                { field: 'userName', align: 'center', title: '验证用户名' },
                { field: 'userNameField', align: 'center', title: "用户名所对应的字段名",width: '10%' },
                { field: 'pswField', align: 'center', title: "密码所对应的字段名", width: '10%'   },
                { field: 'authTokenField', align: 'center', title: "返回Token所在字段" ,width: '10%' },
                { field: 'authTokenHeader', align: 'center', title: "Token所在头" },
                {
                    field: 'bodyType', align: 'center', title: '参数形式', templet: function (d) {
                        if (d.bodyType == 1) {
                            return 'FromBody';
                        } else if (d.bodyType == 2) {
                            return 'FromForm';
                        }
                    }
                },
                { field: 'createTime', align: 'center', title: '创建时间' },
                { templet: '#tableBar', title: '操作', align: 'center', minWidth: 160 }
            ]]
        });

        //监听工具条
        table.on('tool(tableTbBas)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent == "edit") {
                admin.open({
                    area: ["900px", "520px"],
                    type: 1,
                    title: '修改验证配置[' + data.authName + ']',
                    content: $('#modelAuth').html(),
                    success: function (layero, dIndex) {
                        form.render('select');
                        form.val('authEditForm', data);  // 回显数据
                        // 表单提交事件
                        form.on('submit(saveAuth)', function (rowData) {
                            layer.load(2);
                            $.ajax({
                                url: '/api/Quartz/UpdateTaskAuth/' + data.authGuid,
                                type: 'put',
                                contentType: "application/json;charset=utf-8",
                                data: JSON.stringify(rowData.field),
                                dataType: "json",
                                complete: function (obj) {
                                    layer.closeAll('loading');
                                },
                                success: function (res) {
                                    if (res.success) {
                                        layer.msg(res.message, { icon: 1 });
                                        insTb.reload({}, 'data');
                                        layer.close(dIndex);
                                    } else {
                                        layer.msg(res.message, { icon: 2 });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });
            } else if (layEvent == "del") {
                layer.confirm('确定要删除“' + data.authName + '”验证方式吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.load(2);
                    $.ajax({
                        url: '/api/Quartz/DeleteTaskAuth/' + data.authGuid,
                        type: 'delete',
                        contentType: "application/json;charset=utf-8",
                        async: true,
                        dataType: 'json',
                        complete: function (obj) {
                            layer.closeAll('loading');
                        },
                        success: function (result) {
                            layer.closeAll('loading');
                            if (result.success) {
                                layer.msg(result.message, { icon: 1 });
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(result.message, { icon: 2 });
                            }
                        }
                    });
                });
            }
        });

        // 搜索按钮点击事件
        form.on('submit(formSubSearchTbBas)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, 'data');
        });

        //新增任务
        $("#addAuth").click(function () {
            admin.open({
                area: ["900px", "520px"],
                type: 1,
                title: '新验证方式',
                content: $('#modelAuth').html(),
                success: function (layero, dIndex) {
                    form.render('select');
                    // 表单提交事件
                    form.on('submit(saveAuth)', function (rowData) {
                        layer.load(2);
                        $.ajax({
                            url: '/api/Quartz/AddTaskAuth',
                            type: 'post',
                            contentType: "application/json;charset=utf-8",
                            data: JSON.stringify(rowData.field),
                            dataType: "json",
                            async: true,
                            complete: function (obj) {
                                layer.closeAll('loading');
                            },
                            success: function (res) {
                                if (res.success) {
                                    layer.msg(res.message, { icon: 1 });
                                    insTb.reload({}, 'data');
                                    layer.close(dIndex);
                                } else {
                                    layer.msg(res.message, { icon: 2 });
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        });
    });
</script>

